layui.use(['element','table','form','layer'], function(){
    var table = layui.table,
        form = layui.form,
        $ = layui.jquery,
        layer = layui.layer;

    table.render({
        elem: '#deviceTable',
        cellMinWidth: 80,
        url: base_url + "/cdinfogoDevice/pageList", //数据接口
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": 0, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },
        method: 'post',
        page: true,
        limits: [10,20,30,50,80,100],
        cols: [[
            {field: 'xh',type:'numbers', title: '序号',}
            ,{field: 'id', align: 'center', hide: true}
            , {field: 'deviceName', title: '设备名称', align: 'center'}
            , {field: 'deviceType', title: '设备类型', align: 'center'}
            , {field: 'deviceMac', title: '设备MAC', align: 'center'}
            , {field: 'creator', title: '添加人员', align: 'center'}
            , {field: 'createTime', title: '创建时间', align: 'center'}
            ,{field:'right',title:'操作',toolbar:'#barOperate',align:'center',width:220}
        ]]
    });

    table.on('tool(deviceTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case 'programMgr':
                form.render();
                layer.open({
                    type: 1
                    , title: '发布信息'
                    , area: ['55%', '70%']
                    , content: "下发节目"
                });
                break;
            case 'delete':
                layer.confirm("确认删除该设备？", function () {
                    $.ajax({
                        url: "/cdcbiBaseCommunity/del/" + data.id,
                        success: function (data) {
                            if (data.success) {
                                layer.msg(data.msg, {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                            active['reload'].call(this);
                        }
                    });
                });
                break;

        }
    });

    form.on('submit(query_device)',function () {
        table.reload('deviceTable', {
            page: {
                curr: 1
            },
            where: data.field
        }, 'data');
        return false;
    });


    $("#syncDevice").on('click', function (){
        layer.confirm('同步数据，是否继续？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            var index = layer.load(2, {
                shade: [0.4, '#000'],
                content: '同步中...',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'padding-top': '40px',
                        'width': '200px'
                    });
                }
            });
            $.ajax({
                url: base_url + "cdinfogoDevice/updateDevice",
                success: function(res){
                    layer.msg(res.msg)
                    table.reload('deviceTable');
                    layer.close(index);
                }
            });
        })
    });

});

function parseStatus(status) {
    if(typeof status == "undefined" || status == null){
        return "未知";
    }
    switch (Number(status)) {
        case 0 : return "在线";
        case 1 : return "离线";
        default: return "未知";
    }
}
